package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduForumTopicDTO;
import com.zenith.edu.po.vo.EduForumTopicVO;
import com.zenith.edu.service.EduForumTopicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/forum-topic")
@Tag(name = "论坛主题管理", description = "论坛主题管理接口")
public class EduForumTopicController {
    
    @Autowired
    private EduForumTopicService eduForumTopicService;
    
    @SecurePostMapping(value = "/createForumTopic", roles = "ROLE_ADMIN", permission = "forum-topic:create")
    @Operation(summary = "创建论坛主题")
    public Result<EduForumTopicVO> createForumTopic(@RequestBody EduForumTopicDTO dto) {
        EduForumTopicVO forumTopicVO = eduForumTopicService.createForumTopic(dto);
        return Result.success(forumTopicVO);
    }
    
    @SecurePostMapping(value = "/getForumTopicById/{id}", roles = "ROLE_ADMIN", permission = "forum-topic:read")
    @Operation(summary = "根据ID获取论坛主题详情")
    public Result<EduForumTopicVO> getForumTopicById(@PathVariable Long id) {
        EduForumTopicVO forumTopicVO = eduForumTopicService.getForumTopicById(id);
        if (forumTopicVO == null) {
            return Result.fail("论坛主题不存在");
        }
        return Result.success(forumTopicVO);
    }
    
    @SecurePostMapping(value = "/updateForumTopic", roles = "ROLE_ADMIN", permission = "forum-topic:update")
    @Operation(summary = "更新论坛主题信息")
    public Result<EduForumTopicVO> updateForumTopic(@RequestBody EduForumTopicDTO dto) {
        EduForumTopicVO forumTopicVO = eduForumTopicService.updateForumTopic(dto);
        return Result.success(forumTopicVO);
    }
    
    @SecurePostMapping(value = "/deleteForumTopic/{id}", roles = "ROLE_ADMIN", permission = "forum-topic:delete")
    @Operation(summary = "删除指定论坛主题")
    public Result<Void> deleteForumTopic(@PathVariable Long id) {
        boolean result = eduForumTopicService.deleteForumTopic(id);
        if (!result) {
            return Result.fail("论坛主题删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getForumTopicList", roles = "ROLE_ADMIN", permission = "forum-topic:read")
    @Operation(summary = "获取论坛主题列表")
    public Result<List<EduForumTopicVO>> getForumTopicList() {
        List<EduForumTopicVO> forumTopicList = eduForumTopicService.getForumTopicList();
        return Result.success(forumTopicList);
    }
}